<!-- 该画面是项目计划信息客户信息Sheet画面：能够新增、修改客户信息 -->
<template>
  <el-dialog
    :title="title"
    :visible.sync="openSheet"
    width="1100px"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    append-to-body
    :before-close="cancel"
  >
    <div class="DescriptionTitle">
      {{ createFlag ? $t("projectPlanCustomerCreateTitle") : $t("projectPlanCustomerUpdateTitle") }} </div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('customerCode')" prop="customer_code" :error="error.customer_code">
            <el-input v-model="form.customer_code" show-word-limit maxlength="12" clearable :placeholder="$t('inputcustomerCode')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('customerName')" prop="customer_name" :error="error.customer_name">
            <el-input v-model="form.customer_name" show-word-limit maxlength="30" clearable :placeholder="$t('inputcustomerName')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('customerUrl')" prop="customer_url" :error="error.customer_url">
            <el-input v-model="form.customer_url" show-word-limit maxlength="30" clearable :placeholder="$t('inputcustomerUrl')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('salesAmount')" prop="sales_amount" :error="error.sales_amount">
            <el-input v-model="form.sales_amount" :placeholder="$t('inputsalesAmount')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('customerProduct')" prop="customer_product" :error="error.customer_product">
            <el-input v-model="form.customer_product" type="textarea" autosize show-word-limit maxlength="30" clearable :placeholder="$t('inputcustomerProduct')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('productForm')" prop="product_form" :error="error.product_form">
            <el-input v-model="form.product_form" type="textarea" autosize show-word-limit maxlength="30" clearable :placeholder="$t('inputproductForm')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('factoryNumber')" prop="factory_num" :error="error.factory_num">
            <el-input v-model="form.factory_num" :placeholder="$t('inputfactoryNumber')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('stockClustersNumber')" prop="stock_clusters_num" :error="error.stock_clusters_num">
            <el-input v-model="form.stock_clusters_num" :placeholder="$t('inputstockClustersNumber')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('productNumber')" prop="product_num" :error="error.product_num">
            <el-input v-model="form.product_num" :placeholder="$t('inputproductNumber')" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('itemNumber')" prop="item_num" :error="error.item_num">
            <el-input v-model="form.item_num" :placeholder="$t('inputitemNumber')" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('stockAddress')" prop="address" :error="error.address">
            <el-input v-model="form.address" type="textarea" show-word-limit maxlength="50" clearable :placeholder="$t('inputstockAddress')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="createFlag" type="primary" @click="submitForm">{{
        $t('create')
      }}</el-button>
      <el-button v-else type="primary" @click="submitForm">{{
        $t('update')
      }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createProjectPlanCustomerApi, updateProjectPlanCustomerApi } from '../api/projectplancustomer.api';

export default {
  data() {
    return {
      title: undefined,
      openSheet: false,
      createFlag: true,
      customId: undefined,
      form: {
        projectPlanFK: undefined,
        customer_code: undefined,
        customer_name: undefined,
        customer_url: undefined,
        sales_amount: undefined,
        customer_product: undefined,
        product_form: undefined,
        factory_num: undefined,
        stock_clusters_num: undefined,
        address: undefined,
        product_num: undefined,
        item_num: undefined
      },
      error: {
        projectPlanFK: undefined,
        customer_code: undefined,
        customer_name: undefined,
        customer_url: undefined,
        sales_amount: undefined,
        customer_product: undefined,
        product_form: undefined,
        factory_num: undefined,
        stock_clusters_num: undefined,
        address: undefined,
        product_num: undefined,
        item_num: undefined
      },
      rules: {
      }
    };
  },
  computed: {
  },
  methods: {
    // 初始化，data对应的是maintain中的项目计划信息，customedata对应的是maintain中的项目计划客户信息
    async openSheetDialog(data, customdata) {
      this.openSheet = !this.openSheet;
      this.reset();
      if (customdata) {
        this.createFlag = false;
        this.form.projectPlanFK = data.id;
        this.form = { ...customdata };
        this.customId = customdata.id
      } else {
        this.createFlag = true;
        this.form.projectPlanFK = data.id;
        this.customId = undefined
      }
    },
    cancel() {
      this.openSheet = !this.openSheet;
    },
    reset() {
      this.resetForm('form');
      this.resetJsonObject(this.form)
    },
    submitForm: function() {
      this.$refs['form'].validate(async(Valid) => {
        if (Valid) {
          this.resetJsonObject(this.error)
          var resp;
          if (this.createFlag) {
            resp = await createProjectPlanCustomerApi(this.form);
          } else {
            resp = await updateProjectPlanCustomerApi(this.customId, this.form);
          }
          if (resp.success) {
            this.$emit('refreshList');
            this.cancel();
            if (this.createFlag) {
              this.targaMessage(
                this.$t('createSuccess', {
                  title: this.$t('customerCode'),
                  value: this.form.customer_code
                })
              );
            } else {
              this.targaMessage(
                this.$t('updateSuccess', {
                  title: this.$t('customerCode'),
                  value: this.form.customer_code
                })
              );
            }
          } else {
            if (typeof resp.msg === 'object') {
              this.error = resp.msg;
            } else {
              this.targaMessageBox(this, resp.msg);
            }
          }
        }
      });
    }
  }
};
</script>

<style scoped>
/* 调整输入框的字数统计的样式，让其向下、向右移动，不会遮挡输入的文字 */
::v-deep .el-textarea .el-input__count {
  font-size: 10px;
  bottom: -6px;
  right: 2px;
}
</style>
